package com.ltu.timkor6.gamesite.users.client;

import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Composite;
import com.ltu.timkor6.gamesite.shared.UserInfo;

public class SignIn
{
	private Users users;
	private SignInComposite composite;
	
	private UsersServiceAsync usersService = GWT.create(UsersService.class);
	
	public SignIn(Users users)
	{
		this.users = users;
		composite = new SignInComposite(this);
	}
	
	public void checkSubmit()
	{
		boolean error = false;
		
		String username = composite.getUsername();
		String password = composite.getPassword();
		
		composite.clearErrors();
		
		if(username.isEmpty())
		{
			error = true;
			
			composite.addError("Username cannot be empty.");
		}

		if(password.isEmpty())
		{
			error = true;
			
			composite.addError("Password cannot be empty.");
		}
		
		if(!error)
		{
			sendSignIn(username, password);
		}
	}
	
	private void sendSignIn(String username, String password)
	{
		String[] credentials = new String[2];
		credentials[0] = username;
		credentials[1] = password;
		
		// Initialize the service proxy.
		if(usersService == null)
		{
			usersService = GWT.create(UsersService.class);
		}
		
		// Set up the callback object.
		AsyncCallback<SignInStatus> callback = new AsyncCallback<SignInStatus>()
		{
			public void onFailure(Throwable caught)
			{
				Window.alert("Error: " + caught.getMessage());
			}
			
			public void onSuccess(SignInStatus status)
			{
				if(status == SignInStatus.SUCCESS)
				{
					composite.clear();
					checkIfLoggedIn(SignInTypes.GET);
				}
				else if(status == SignInStatus.NO_USER)
				{
					composite.clearPassword();
					composite.addError("Not such user.");
				}
				else if(status == SignInStatus.NO_PASS)
				{
					composite.clearPassword();
					composite.addError("No password.");
				}
				else if(status == SignInStatus.NO_MATCH)
				{
					composite.clearPassword();
					composite.addError("Wrong username or password.");
				}
				else
				{
					composite.addError("Something went wrong with the server (" + SignInStatus.UNKNOWN + ").");
				}
			}
		};
		
		// Make the call to the stock price service.
		usersService.signIn(credentials, callback);
	}
	
	/**
	 * Used to check if a user is signed in at the loading of the web site.
	 */
	public void checkIfLoggedIn(final SignInTypes type)
	{
		// Initialize the service proxy.
		if(usersService == null)
		{
			usersService = GWT.create(UsersService.class);
		}
		
		// Set up the callback object.
		AsyncCallback<UserInfo> callback = new AsyncCallback<UserInfo>()
		{
			public void onFailure(Throwable caught)
			{
				Window.alert("Error: " + caught.getMessage());
			}
			
			public void onSuccess(UserInfo userInfo)
			{
				switch(type)
				{
					// Change the layout depending of sign in status
					case CHECK:
						users.checkIfLoggedIn(userInfo);
						break;
					// When signing in, change userInfo and the layout
					case GET:
						users.checkUserInfo(userInfo);
						break;
					// Updated userInfo
					case UPDATED:
						users.updateProfile(userInfo);
						break;
					// Update session on the server side
					case SESSION:
						break;
				}
			}
		};
		
		// Make the call to the stock price service.
		usersService.getUserInfo(callback);
	}
	
	public Composite getComposite()
	{
		return composite;
	}
}
